//
//  NSString+DZCategory.m
//  Meidaojia
//
//  Created by Darren on 15/5/20.
//  Copyright (c) 2015年 Darren Zheng. All rights reserved.
//

#import "NSString+DZCategory.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (DZCategory)


- (NSString *)MD5String {
    const char *cstr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cstr, (CC_LONG)strlen(cstr), result);
    
    return [NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];  
}

- (CGSize)sizeForMaxWidth:(CGFloat)width
                     font:(UIFont *)font
{
    return [self sizeForMaxWidth:width font:font numberOfLines:0];
}

- (CGSize)sizeForMaxWidth:(CGFloat)width
                     font:(UIFont *)font
            numberOfLines:(int)numberOfLines
{
    CGRect rect = [self boundingRectWithSize:CGSizeMake(width, numberOfLines == 0 ? CGFLOAT_MAX : [font pointSize] * (numberOfLines + 1))
                                     options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: font}
                                     context:nil];
    rect.size.width = ceil(rect.size.width);
    rect.size.height = ceil(rect.size.height);
    return rect.size;
}

- (CGSize)sizeWithFont:(UIFont *)font
{
    return [self sizeWithAttributes:@{NSFontAttributeName:font}];
}

- (NSMutableDictionary *)queryComponents
{
    NSArray *queryComp = [self componentsSeparatedByString:@"&"];
    NSMutableDictionary *parameters = [NSMutableDictionary new];
    for (NSString *component in queryComp) {
        NSArray *subcomponents = [component componentsSeparatedByString:@"="];
        [parameters setObject:[[subcomponents objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                       forKey:[[subcomponents objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    return parameters;
}

//将字符串转int
-(int)IntWithString:(NSString *)String{
    NSScanner *scanner = [NSScanner scannerWithString:String];
    [scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
    int number;
    [scanner scanInt:&number];
    return number;
}

+(BOOL)isNull:(NSString *)string
{
    if (string == nil) {
        return YES;
    }
    if (string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([string isEqualToString:@"(null)"]) {
        return YES;
    }
    if ([string isEqualToString:@"<null>"]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}
//在字符串sting1 中将所有string2  替换成string3
-(NSString *)stringByReplacingCharactersInRangeString:(NSString *)string1 andString:(NSString *)string2 andorString:(NSString *)string3{
    
    NSUInteger cnt = 0, length = [string1 length];
    NSRange range = NSMakeRange(0, length);
    while(range.location != NSNotFound)
    {
        range = [string1 rangeOfString: string2 options:0 range:range];
        
        if(range.location != NSNotFound)
        {
            NSString *string5 = [string1 substringWithRange:NSMakeRange(range.location,10)];
            if(![string5 hasSuffix:@"http:"]){
                string1 = [string1 stringByReplacingCharactersInRange:range withString:string3];
            }
            range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
            cnt++;
        }
    }
    return string1;
}

#pragma mark - ******************** 拼接html语言
+ (NSString *)showInWebView:(NSString *)toBody
{
    NSMutableString *html = [NSMutableString string];
    [html appendString:@"<html>"];
    [html appendString:@"<head>"];
    [html appendString:@"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\">"];
    [html appendString:@"<meta name=\"renderer\" content=\"webkit\">"];
    [html appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">",[[NSBundle mainBundle] URLForResource:@"SXDetails.css" withExtension:nil]];
    [html appendString:@"<style>"];
    [html appendString:@"img{width:100%;}"];
    [html appendString:@"</style>"];
    [html appendString:@"</head>"];
    if (IS_IPHONE_4_OR_LESS ||IS_IPHONE_5) {
        [html appendString:@"<body style=\"width:80%;background:#ffffff;font-size:16px;\">"];
    }else if (IS_IPHONE_6){
        [html appendString:@"<body style=\"width:90%;background:#ffffff;font-size:16px;\">"];
    }
    else{
        [html appendString:@"<body style=\"width:95%;background:#ffffff;font-size:16px;\">"];
    }
    [html appendString:toBody];
    [html appendString:@"</body>"];
    [html appendString:@"</html>"];
    return html;
}



@end
